﻿layui.use(['form', 'layer', 'table', 'laytpl', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;


    function loadDate() {
        laydate.render({
            elem: '#startdate'
        });
        laydate.render({
            elem: '#enddate'
        });
    }
    
    var tableIns = table.render({
        elem: '#managerList',
        url: '/InOutCash/LoadGomePayOutCashRecordData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "managerListTable",
        cols: [[
            { type: "checkbox", fixed: "left", width: 50 },
            { field: "Id", title: '序号', width: 70, align: "center" },
            { field: 'OrderNumber', title: '订单号', width: 220, align: "center"},
            { field: 'Amount', title: '金额(RMB)',width: 120, align: "center"},
            { field: 'CustomerName', title: '帐户名',width: 150, align: "center", templet: '<span>{{ d.CustomerName }}({{ d.UserName }})</span>' },
            { field: 'AccountNumber', title: '银行卡号', width: 180, align: "center" },
            { field: 'IssueBankName', title: '开户行', width: 120, align: "center" }, 
            { field: 'GPOrderId', title: '美付宝订单号', width: 220, align: "center" }, 
            { field: 'AddTime', title: '提交时间', width: 180, align: "center" },
            { field: 'State', title: '状态', width: 150, align: "center", templet: '#State' },
            { title: '操作', minWidth: 250, templet: '#managerListBar', fixed: "right", align: "center" }


        ]],
        totalRow: false  //开启合计行
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        //if ($(".searchVal").val() !== '') {
        table.reload("managerListTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                key: $(".searchVal").val(),  //搜索的关键字
                state: $(".searchState").val(),
                start: $(".startdate").val(),
                end: $(".enddate").val(),
                managerId: $(".searchManagerId").val()
            }
        });
        //} else {
        //    layer.msg("请输入搜索的内容");
        //}
    });

    loadDate();

    table.on('tool(managerList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'queryResult') { //审核并打款
            queryGomePayResult(data);
        }
    });

    function queryGomePayResult(item){
        var loadingIndex = layer.load(3, {
            shade: 0.8
        });
        $.ajax({
            type: 'POST',
            url: '/InOutCash/GetGomePayResultByOrderNo/?orderNo=' + item.OrderNumber ,
            data: {},
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (data) {
                if (data.Success == true && data.Data != null) {
                    var item = data.Data;
                    var showHtml = " <div>定单号：" + item.OrderNumber + "</div><div>美付宝单号：" + item.OrderId + "</div><div>交易金额：" + item.Amount + "</div><div>货币单位：" + item.Currency + "</div><div>交易时间：" + item.DealTime + "</div>";
                    layer.alert(showHtml);
                }else{
                    layer.alert(data.ResultMsg);
                }
                layer.close(loadingIndex);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                layer.close(loadingIndex);
            }
        });
    }

});